<template>
  <!-- 新增优惠券 -->
  <div>
    <!-- 新增，返回按钮 -->
    <div style="display: flex; justify-content: space-between">
      <div>
        <el-button type="primary" @click="openDialog('新增')">
          添加优惠券
        </el-button>
      </div>
      <el-button @click="$emit('send')">返回</el-button>
    </div>
    <!-- 搜索，筛选 -->
    <div style="margin-top: 10px">
      <el-input
        v-model="filterInp"
        placeholder="请输入优惠券名称"
        style="width: 17%; margin-right: 10px"
      />
      <el-button
        :loading="filterLoad"
        type="primary"
        @click="getCouponIist('filterLoad')"
      >
        筛选
      </el-button>
    </div>
    <!-- 优惠券展示列表 -->
    <div style="margin-top: 10px">
      <!-- 优惠券展示表格 -->
      <el-table :data="couponList" :empty-text="emptyText">
        <el-table-column label="优惠券名称" prop="coupon_name" />
        <el-table-column label="优惠券类型" prop="coupon_type_name" />
        <el-table-column label="用卷次数" prop="limit_num" />
        <el-table-column label="剩余卷数量" prop="surplus_num" />
        <el-table-column label="已使用" prop="use_num" />
        <el-table-column label="用卷时间" prop="start_at" />
        <el-table-column label="结束时间" prop="end_at" />
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" @click="openDialog('编辑', scope.row)">
              编辑
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div style="display: flex; justify-content: space-between">
        <div></div>
        <el-pagination
          :current-page.sync="page"
          layout="prev, pager, next, jumper"
          :total="total"
          @current-change="getCouponIist"
        />
      </div>
    </div>
    <!-- 添加优惠券 -->
    <el-dialog :title="dialogTitle + '优惠券'" :visible.sync="dialogStoreVal">
      <Add-coupon
        v-if="dialogStoreVal"
        :o-coupon-id="couponId"
        :o-inp-bool="inpBool"
        @send="closeDialog"
      />
    </el-dialog>
  </div>
</template>

<script>
  import { getCouponList } from '@/api/coupon'

  export default {
    components: {
      AddCoupon: () => import('./components/addcoupon.vue'),
    },
    data() {
      return {
        filterInp: '', // 搜索关键字
        filterLoad: false, // 筛选按钮
        emptyText: ' ',
        couponList: [], // 存放获取到的优惠券列表
        page: 1,
        total: 10,

        couponId: 0, // 存放选中的优惠券id

        dialogStoreVal: false, // 控制添加优惠券对话框显示隐藏
        dialogTitle: '', // 对话框标题
        inpBool: false,
      }
    },
    mounted() {
      this.getCouponIist()
    },
    methods: {
      // 获取优惠券列表
      getCouponIist(btnLoad) {
        const loading = this.$loading({
          lock: true,
          target:
            this.couponList.length == 0
              ? '.el-table__empty-block'
              : '.el-table__body',
        })
        this[btnLoad] = true
        const data = {
          coupon_name: this.filterInp,
          page: this.page,
        }
        getCouponList(data).then((res) => {
          this.couponList = res.data
          loading.close()
          this[btnLoad] = false
          if (res.data.length == 0) {
            this.emptyText = '暂无数据'
          }
        })
      },
      // 根据参数打开对话框
      openDialog(name, row) {
        this.dialogTitle = name
        this.dialogStoreVal = true
        if (name == '新增') {
          this.couponId = 0
          this.inpBool = false
        } else {
          this.couponId = row.id
          this.inpBool = true
        }
      },
      // 关闭对话框
      closeDialog() {
        this.dialogStoreVal = false
        this.getCouponIist()
      },
    },
  }
</script>

<style scoped></style>
